package mortal.game.control
{
    import fl.data.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.*;
    import mortal.game.view.smelt.data.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class SmeltController extends Controller
    {
        private var _smeltModule:SmeltModule;
        private var _currentSmeltPlan:SmeltPlanData;
        private var _currentMaxSmeltNum:int = 1;
        private var _currentSmeltNum:int = 1;

        public function SmeltController()
        {
            
        }

        public function get currentMaxSmeltNum() : int
        {
            if (this._currentMaxSmeltNum < 1)
            {
                return 1;
            }
            if (this._currentMaxSmeltNum > 99)
            {
                return 99;
            }
            return this._currentMaxSmeltNum;
        }

        public function get currentSmeltNum() : int
        {
            if (this._currentSmeltNum < 1)
            {
                return 1;
            }
            if (this._currentSmeltNum > this.currentMaxSmeltNum)
            {
                return this.currentMaxSmeltNum;
            }
            return this._currentSmeltNum;
        }

        public function set currentSmeltNum(param1:int) : void
        {
            this._currentSmeltNum = param1;
            
        }

        private function onSmeltPlanChange(event:DataEvent) : void
        {
            this.doChangeSmeltPlan(event.data as SmeltPlanData);
            
        }

        private function doChangeSmeltPlan(param1:SmeltPlanData = null, param2:Boolean = true) : void
        {
            if (this._smeltModule == null)
            {
                
            }
            this._currentSmeltPlan = param1;
            if (this._currentSmeltPlan)
            {
                this.countPlanSmeltNum(this._currentSmeltPlan);
                this._currentMaxSmeltNum = this._currentSmeltPlan.canSmeltNum;
                if (param2)
                {
                    this.currentSmeltNum = this.currentMaxSmeltNum;
                }
                this._smeltModule.setMaterials(this._currentSmeltPlan.materials);
                this._smeltModule.setMainProduce(this._currentSmeltPlan.mainProduce, this._currentSmeltPlan.smeltPlanDesc);
                this._smeltModule.setSuccessRate(this._currentSmeltPlan.mainProduceRate);
                this._smeltModule.setCost(this._currentSmeltPlan.smeltPlanCoin * this.currentSmeltNum, this.isEnoughMoney());
                this._smeltModule.setSmeltNum(this.currentSmeltNum);
            }
            else
            {
                this._currentMaxSmeltNum = 0;
                this._smeltModule.setMaterials([]);
                this._smeltModule.setMainProduce(null, "");
                this._smeltModule.setSuccessRate(0);
                this._smeltModule.setCost(0, true);
                this._smeltModule.setSmeltNum(1);
            }
            
        }

        private function onSmeltButtonClick(event:DataEvent) : void
        {
            if (view.isHide)
            {
                view.show();
            }
            else
            {
                view.hide();
            }
            
        }

        private function onCountPlanListNum(event:DataEvent = null) : void
        {
            var _loc_2:DataProvider = null;
            var _loc_3:SmeltPlanData = null;
            var _loc_4:int = 0;
            if (this._smeltModule)
            {
                _loc_2 = this._smeltModule.currentShowedPlanListData;
                _loc_4 = 0;
                while (_loc_4 < _loc_2.length)
                {
                    
                    _loc_3 = _loc_2.getItemAt(_loc_4) as SmeltPlanData;
                    this.countPlanSmeltNum(_loc_3);
                    _loc_4++;
                }
                this._smeltModule.refreshPlanList();
            }
            
        }

        private function onStartSmelt(event:DataEvent = null) : void
        {
            if (this.checkCanSmelt())
            {
                GameProxy.smelt.smelt(this._currentSmeltPlan.smeltPlanCode, this.currentSmeltNum);
            }
            
        }

        private function onSmeltSuccess(event:DataEvent) : void
        {
            MsgManager.addBroadCast("恭喜你，炼化成功！");
            this.doChangeSmeltPlan(this._currentSmeltPlan, false);
            
        }

        private function onSmeltFailed(event:DataEvent) : void
        {
            
        }

        private function onSmeltNumChange(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            this.currentSmeltNum = _loc_2;
            this.doChangeSmeltPlan(this._currentSmeltPlan, false);
            
        }

        private function serverBagDataChangehandler(param1:Object = null) : void
        {
            if (this._currentSmeltPlan)
            {
                this.onCountPlanListNum();
                this.doChangeSmeltPlan(this._currentSmeltPlan, false);
            }
            
        }

        private function updateCost(param1:Object = null) : void
        {
            if (this._smeltModule && this._currentSmeltPlan)
            {
                this._smeltModule.setCost(this._currentSmeltPlan.smeltPlanCoin, this.isEnoughMoney());
            }
            
        }

        private function countPlanSmeltNum(param1:SmeltPlanData) : void
        {
            var _loc_2:int = 0;
            var _loc_3:SmeltMaterialItemData = null;
            var _loc_4:int = 0;
            while (_loc_4 < param1.materials.length)
            {
                
                _loc_3 = param1.materials[_loc_4] as SmeltMaterialItemData;
                this.countOwnItemAmount(_loc_3);
                if (_loc_4 == 0)
                {
                    _loc_2 = Math.floor(_loc_3.itemAmountOwn / _loc_3.itemAmountNeeded);
                }
                else
                {
                    _loc_2 = Math.min(this._currentMaxSmeltNum, Math.floor(_loc_3.itemAmountOwn / _loc_3.itemAmountNeeded));
                }
                _loc_4++;
            }
            param1.canSmeltNum = _loc_2;
            
        }

        private function countOwnItemAmount(param1:SmeltMaterialItemData) : SmeltMaterialItemData
        {
            var _loc_3:ItemData = null;
            param1.itemAmountOwn = 0;
            var _loc_2:* = cache.pack.backPackCache.getAllItems();
            var _loc_4:int = 0;
            while (_loc_4 < _loc_2.length)
            {
                
                _loc_3 = _loc_2[_loc_4] as ItemData;
                if (_loc_3 && (_loc_3.itemInfo.item.code == param1.itemBindCode || _loc_3.itemInfo.item.code == param1.itemUnBindCode))
                {
                    param1.itemAmountOwn = param1.itemAmountOwn + _loc_3.itemAmount;
                }
                _loc_4++;
            }
            return param1;
        }

        private function isEnoughMoney() : Boolean
        {
            var _loc_1:int = 0;
            if (this._currentSmeltPlan)
            {
                _loc_1 = cache.role.money.coinBind + cache.role.money.coin;
                return _loc_1 >= this._currentSmeltPlan.smeltPlanCoin * this.currentSmeltNum;
            }
            return true;
        }

        private function isEnoughMaterial() : Boolean
        {
            var _loc_1:SmeltMaterialItemData = null;
            var _loc_2:int = 0;
            if (this._currentSmeltPlan)
            {
                _loc_2 = 0;
                while (_loc_2 < this._currentSmeltPlan.materials.length)
                {
                    
                    _loc_1 = this._currentSmeltPlan.materials[_loc_2] as SmeltMaterialItemData;
                    if (_loc_1.itemAmountOwn < _loc_1.itemAmountNeeded * this.currentSmeltNum)
                    {
                        return false;
                    }
                    _loc_2++;
                }
                return true;
            }
            return false;
        }

        private function checkCanSmelt() : Boolean
        {
            if (this._currentSmeltPlan == null)
            {
                MsgManager.addBroadCast("请先选择炼化目标");
                return false;
            }
            if (!this.isEnoughMaterial())
            {
                MsgManager.addBroadCast("炼化材料不足");
                return false;
            }
            if (!this.isEnoughMoney())
            {
                MsgManager.addBroadCast("铜钱不足，无法炼化");
                return false;
            }
            return true;
        }

        override protected function initView() : IView
        {
            if (!this._smeltModule)
            {
                this._smeltModule = new SmeltModule();
                this.onCountPlanListNum();
            }
            return this._smeltModule;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.SmeltSmeltButtonClick, this.onSmeltButtonClick);
            Dispatcher.addEventListener(EventName.SmeltCountPlanListNum, this.onCountPlanListNum);
            Dispatcher.addEventListener(EventName.SmeltSelectedPlanChange, this.onSmeltPlanChange);
            Dispatcher.addEventListener(EventName.SmeltStartSmelt, this.onStartSmelt);
            Dispatcher.addEventListener(EventName.SmeltSmeltSuccess, this.onSmeltSuccess);
            Dispatcher.addEventListener(EventName.SmeltSmeltFailed, this.onSmeltFailed);
            Dispatcher.addEventListener(EventName.SmeltSmeltNumChange, this.onSmeltNumChange);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.updateCost);
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.serverBagDataChangehandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            
        }

    }
}
